<?php
/*
 * @Author: your name
 * @Date: 2021-02-22 12:51:29
 * @LastEditTime: 2021-02-23 20:38:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \lh_vue\awesomepos\php\Db.php
 */
class Db{
    protected $db_host;
    protected $db_user;
    protected $db_pass;
    protected $db_name;

    public function __construct(){
        $this->db_host = 'localhost';
        $this->db_user = 'root';
        $this->db_pass = 'root';
        $this->db_name = 'bd_fresh';
    }

    //获取句柄对象
    public function getDB(){
        $mysqli = new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);
        if(mysqli_connect_errno()){
            echo '数据库连接错误，错误信息是'.mysqli_connect_error();
            exit();
        }
        //设置编码
        $mysqli->set_charset('utf8');
        return $mysqli;
    }

    //增加  修改  删除
    public function aud($_sql){
        $_db = $this->getDB();
        $_db->query($_sql);
        $_affected_rows = $_db->affected_rows;
        $result = '';
        $this->unDB($result,$_db);
        return $_affected_rows; 
    }


     

  


    //释放结果集
    public function unDB($res=null,&$mysqli){
        if(is_object($res)){
            //释放结果集
            $res->free();
            //清空结果集
            $res = null;
        }
        if(is_object($mysqli)){
            //关闭数据库
            $mysqli->close();
            //清楚数据库信息
            $mysqli = null;
        }
    }
}